<template>
  <div class="app-container">

    <el-form :model="form" ref="form" label-width="100px" v-loading="formLoading">

      <el-form-item label="校区：" required>
        <el-select v-model="form.school" placeholder="校区">
          <el-option v-for="item in schoolEnum" :key="item.key" :value="item.key" :label="item.value"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="学科：" required>
        <el-select v-model="form.level" placeholder="学科">
          <el-option v-for="item in levelEnum" :key="item.key" :value="item.key" :label="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开班日期：" required>
        <el-date-picker
          v-model="form.openingTime"
          type="date"
          placeholder="选择日期">
        </el-date-picker>
        <!--<el-input v-model="form.name"></el-input>-->
        <!--<template slot="label">
            <span>年级
              <el-tooltip class="item" effect="dark" placement="right">
                <i class="el-icon-question" style="font-size: 16px; vertical-align: middle;"></i>
                <div slot="content">
                  <p>举例：</p>
                  <p>选择是：计量数据大于0，将会忽略计量数据与公式的计算，直接计算整个时段(计量周期)费用。如：按日月结选择是，用1秒也按1天计费。</p>
                  <p>选择否：计量数据与配置的公式进行计算。</p>
                </div>
              </el-tooltip>
            </span>
        </template>-->

      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('subjectForm')">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapGetters, mapState, mapActions } from 'vuex'
import subjectApi from '@/api/subject'

export default {
  data () {
    return {
      form: {
        id: null,
        name: '',
        level: '',
        levelName: '',
        school:'',
        schoolName:'',
        openingTime:'',
      },
      formLoading: false
    }
  },
  created () {
    let id = this.$route.query.id
    let _this = this
    if (id && parseInt(id) !== 0) {
      _this.formLoading = true
      subjectApi.select(id).then(re => {
        _this.form = re.response
        _this.formLoading = false
      })
    }
  },
  methods: {
    submitForm (formName) {
      if (!this.form.school){
        this.$message.error('请选择校区');
        return;
      }
      if (!this.form.level){
        this.$message.error('请选择学科');
        return;
      }

      if (!this.form.openingTime){
        this.$message.error('请选择开班时间');
        return;
      }

      let _this = this
      this.formLoading = true
      this.form.levelName = this.enumFormat(this.levelEnum, this.form.level)
      this.form.schoolName = this.enumFormat(this.schoolEnum, this.form.school)
      this.form.name = this.form.schoolName + "-" + this.form.levelName + "-" + this.formatTime(this.form.openingTime)
      subjectApi.edit(this.form).then(data => {
        if (data.code === 1) {
          _this.$message.success(data.message)
          _this.delCurrentView(_this).then(() => {
            _this.$router.push('/education/subject/list')
          })
        } else {
          _this.$message.error(data.message)
          _this.formLoading = false
        }
      }).catch(e => {
        _this.formLoading = false
      })
    },
    //临时格式化时间
    formatTime(time){

      const y = time.getFullYear() // 可返回一个表示年份的 4 位数字
      const m = time.getMonth() + 1 < 10 ? '0' + (time.getMonth() + 1) : time.getMonth() + 1  // 可返回表示月份的数字。返回值是 0（一月） 到 11（十二月） 之间的一个整数 // 注意： 一月为 0, 二月为 1, 以此类推。
      const d = time.getDate() < 10 ? '0' + time.getDate() : time.getDate()
      //const h = time.getHours() < 10 ? '0' + time.getHours() : time.getHours()
      //const min = time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes()
      //const seconds = time.getSeconds() < 10 ? '0' + time.getSeconds() : time.getSeconds()
      //return y + '-' + m + '-' + d + ' ' + h + ':' + min + ':' + seconds
      return y + '-' + m + '-' + d;
    },
    resetForm () {
      let lastId = this.form.id
      this.$refs['form'].resetFields()
      this.form = {
        id: null,
        name: '',
        level: 1,
        levelName: ''
      }
      this.form.id = lastId
    },
    ...mapActions('tagsView', { delCurrentView: 'delCurrentView' })
  },
  computed: {
    ...mapGetters('enumItem', [
      'enumFormat'
    ]),
    ...mapState('enumItem', {
      levelEnum: state => state.user.levelEnum,
      schoolEnum: state => state.user.schoolEnum
    })
  }
}
</script>
